package com.stu.status;

import java.util.ArrayList;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.stu.aq.*;
import com.stu.widget.*;

public class MotorsStatusFragment extends Fragment {
	private ArrayList<MotorBarView> motorBars;
	private int scaleFactor;

	public MotorsStatusFragment() {
		motorBars = new ArrayList<MotorBarView>(AQ.motorCount);
		scaleFactor = (AQ.motorMax - AQ.motorMin) / 100;
		
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	  Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.motor_status, container, false);
		// use add with index for reuse when view is recreated
		MotorBarView m = (MotorBarView)v.findViewById(R.id.motorStatusBar1);
		m.setEnabled( false );
		motorBars.add( 0, m );
		m = (MotorBarView)v.findViewById(R.id.motorStatusBar2);
		m.setEnabled( false );
		motorBars.add( 1, m );
		m = (MotorBarView)v.findViewById(R.id.motorStatusBar3);
		m.setEnabled( false );
		motorBars.add( 2, m );
		m = (MotorBarView)v.findViewById(R.id.motorStatusBar4);
		m.setEnabled( false );
		motorBars.add( 3, m );
		return v;
	}
	
	/**
	 * Set the motor bar value
	 * 
	 * @param motor Index of motor bar to set
	 * @param value Value from AQ.motorMin to AQ.motorMax
	 */
	public void setValue( int motor, int value ) {
		motorBars.get(motor).setValue( (value - AQ.motorMin) / scaleFactor );
	}
	
	public void clear() {
		for( MotorBarView m : motorBars )
			m.setValue( 0 );
	}
}
